package com.youhong.teethcare.settingpage;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.youhong.teethcare.BaseActivity;
import com.youhong.teethcare.R;
import com.youhong.teethcare.customviews.BrushHeadReminderCircle;
import com.youhong.teethcare.javabeans.BrushingRecord;
import com.youhong.teethcare.services.Common;
import com.youhong.teethcare.utils.DBHelper;
import com.youhong.teethcare.utils.DialogUtil;
import com.youhong.teethcare.utils.Util;

import java.util.Calendar;
import java.util.List;

public class BrushHeadActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brush_head);
        initStatusBar(getResources().getColor(R.color.pageBackground),true);

        getViews();
        initializeVariables();
    }
    private long getChangeDays(long currentTime,long changeTime){
        long days=0;
        long duration=currentTime-changeTime;
        days=duration/(24*60*60*1000l);
        return  90-days;
    }
    Button btn_reset;
    TextView tv_totalBrushSession, tv_totalTime, tv_lossRate, tv_headChangeTime;
    ImageView iv_back;
    TextView tvRemainingTime;
    BrushHeadReminderCircle brushHeadReminderCircle;
    private void getViews() {

        iv_back = findViewById(R.id.personalInfo_iv_back);
        iv_back.setOnClickListener(this);
        btn_reset = findViewById(R.id.brushHead_btn_reset);
        btn_reset.setOnClickListener(this);
        brushHeadReminderCircle = findViewById(R.id.BrushHeadReminderCircle);
        tvRemainingTime = findViewById(R.id.tvRemainingTime);

        tv_headChangeTime = findViewById(R.id.brushHead_tv_headChangeTime);
        tv_lossRate = findViewById(R.id.brushHead_tv_lossRate);
        tv_totalBrushSession = findViewById(R.id.brushHead_tv_brushSession);
        tv_totalTime = findViewById(R.id.brushHead_tv_totalTime);
    }


    int lossRate = 0;
    private void initializeVariables() {

        Calendar c = Calendar.getInstance();
        long currentTime=c.getTimeInMillis();
        long changeTime=Common.userInfo.getChangeHead();
        c.setTimeInMillis(changeTime);
        long days=getChangeDays(currentTime,changeTime);
        tvRemainingTime.setText(days+"");
        brushHeadReminderCircle.update(days/90f);
        String changeTimeString = Util.FormDateTimeString2(c);
        tv_headChangeTime.setText(changeTimeString);

        List<BrushingRecord> list = DBHelper.getDbHelper(getApplicationContext()).queryBrushRecordAfterTime(c);

        int totalTimes = 0;
        for (BrushingRecord record : list) {

            totalTimes += record.getTotalTime();
        }

        tv_totalBrushSession.setText(list.size() + "");

        int hours = totalTimes / 60 / 60;
        float minutes = Math.round ((float)(totalTimes / 60 % 60));
        tv_totalTime.setText(hours + "." + (int) (minutes * 10));

        //刷头损耗率算法：0.5 * 使用次数 + 0.2 * 使用总时长

        lossRate = (int) (list.size() * 0.5f + minutes * 0.2f);


        if (lossRate > 100) {
            lossRate = 100;
        }

        tv_lossRate.setText(lossRate + "");
    }

    @Override
    public void onClick(View v) {

        if (iv_back == v) {
            this.finish();
        } else if (v == btn_reset) {
            String text = String.format(getResources().getString(R.string.strings101), lossRate + "%");

            DialogUtil.showModalDialog(BrushHeadActivity.this, text
                    , R.string.strings84, R.string.strings102, R.string.strings99
                    , new Runnable() {
                        @Override
                        public void run() {

                            Common.userInfo.setChangeHead(Calendar.getInstance().getTimeInMillis());
                            Common.userInfo.SyncToLocal(getApplicationContext());

                            Calendar c = Calendar.getInstance();
                            c.setTimeInMillis(Common.userInfo.getChangeHead());
                            String changeTime = Util.FormDateTimeString2(c);
                            tv_headChangeTime.setText(changeTime);
                            initializeVariables();
                            Toast.makeText(BrushHeadActivity.this,
                                    R.string.strings64, Toast.LENGTH_SHORT).show();

                        }
                    }, null
            );
        }
    }
}
